package il.ac.braude.sona.gui.forms.questions;

import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
/**
 * The QComboBox contains all methods for creating ComboBox question in formPanel
 *  
 * @author Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 */
public class QComboBox extends FormQuestion {
	/**
	 * list - the JComboBox of this question
	 */
	private JComboBox list;
	/**
	 * length - the length of combobox list
	 */
	private int length;

	/**
	 * Constructs the QComboBox.
	 * 
	 * @param question - the label of this question
	 * @param comboList - the list to insert into the combobox
	 */
	public QComboBox(String question,String[] comboList) {
		super(BoxLayout.X_AXIS);
		length = comboList.length;
		JLabel q = new JLabel(question);
		add(q);
		list = new JComboBox(comboList);
		add(list);
	}
	/**
	 * Constructs the QComboBox.
	 * 
	 * @param question - the label of this question
	 * @param comboList - the list to insert into the combobox
	 * @param listen - Action Listener for the ComboBox
	 */
	public QComboBox(String question,String[] comboList,ActionListener listen) {
		super(BoxLayout.X_AXIS);
		JLabel q = new JLabel(question);
		add(q);
		list = new JComboBox(comboList);
		list.addActionListener(listen);
		add(list);
	}
	
	/**
	 * Constructs the QComboBox.
	 * 
	 * @param question - the label of this question
	 * @param comboList - the list to insert into the combobox
	 * @param deafualtindex - the deafualt index to view from the comboList
	 */
	public QComboBox(String question,String[] comboList,int deafualtindex) {
		super(BoxLayout.X_AXIS);
		JLabel q = new JLabel(question);
		add(q);
		list = new JComboBox(comboList);
		list.setSelectedIndex(deafualtindex);
		add(list);
	}
	/**
	 * Constructs the QComboBox of numbers only
	 * 
	 * @param question - the label of this question
	 * @param start - the first number in the comboList
	 * @param end - the last number in the comboList
	 */
	public QComboBox(String question,int start,int end) {
		super(BoxLayout.X_AXIS);
		JLabel q = new JLabel(question);
		add(q);
		list = new JComboBox();
		while (start<=end)
			list.addItem(Integer.toString(start++));
		add(list);
	}
	

	/**
	 * Set wether the question is editable
	 * 
	 * @param isEditable - boolean true or false
	 */
	public void setEditable(boolean isEditable) {
		list.setEditable(isEditable);
	}

	@Override
	public void setDefaultAnswer(Object defaultAnswer) {
		list.setSelectedIndex((Integer)defaultAnswer);
	}

	@Override
	public Object getAnswer() {
		return list.getSelectedItem();
	}
	
	/**
	 * Add action listener for the combobox
	 * 
	 * @param listen - Action Listener for the ComboBox
	 */
	public void addActionListener(ActionListener listen) {
		list.addActionListener(listen);
		
	}
	
	/**
	 * remove item from the combobox
	 * 
	 * @param index - The index of item asked to remove
	 */
	@Override
	public void remove(int index) {
		list.remove(index);
		length--;
		
	}
	/**
	 * returns the number of elements in the combobox list
	 * 
	 * @return length - The number of elements in the combobox list
	 */
	public int qSize() {
		return length;
	}
	/**
	 * returns the current selected index in the combobox list
	 * 
	 * @return list.getSelectedIndex() -the current selected index in the combobox list
	 */
	public int getSelected() {
		return list.getSelectedIndex();
		
	}

}
